﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace MultyLanguage
{
    public partial class NetUtil : System.Web.UI.Page
    {
        public static string[]getAllControl(string tags, string path, string defaultpath) // return array of tags, each tag we have an array={id,name,att(value, innerHTML)}
        {
            string _taa = tags;
            string pagename = path.Substring(path.LastIndexOf("/") + 1);
            StreamReader strr = new StreamReader(path);
            string source = strr.ReadToEnd();
            strr.Close();
            tags = "<" + tags.Trim();
            string[] findlength = source.Split(new string[] { tags }, StringSplitOptions.None);
            int existcount = findlength.Length - 1;                 //get count of tag in source
            if (existcount > 0)
            {
                string[] s = new string[existcount];
                // 
                string asp = "<%";
                for (int i = 1; i < findlength.Length; i++)
                {
                    //find id
                   
                    string[] arr3 = findlength[i].Split(new char[] { '>' });
                    string[] arr1 = arr3[0].Split(new string[] { "id","ID","iD","Id" }, StringSplitOptions.None);
                    string[] arr2;

                    if (arr1.Length >= 2)
                    {
                        arr2 = arr1[1].Split(new char[] { '"' }, StringSplitOptions.None);
                        s[i -1] = arr2[1];
                        
                        
                        //check whether default value are saved, if not, create file and save these value
                        string[] bf = findlength[i].Split(new char[] { '>' });
                        string txtvalue = " ";
                        if (findlength[i].Contains("</" + _taa.Trim()))
                        {
                            string[] txtarr2 = bf[1].Split(new char[] { '<' });
                            txtvalue = txtarr2[0].Trim();
                            if (txtvalue != ""  &&  XMLUltility.SetValue("item", s[i - 1], txtvalue, defaultpath) == true)  // just add one more
                            {
                                asp += "this." + s[i - 1] + ".Text=MultyLanguage.NetUtil.Translate(\"" + s[i - 1] + "\",Server.MapPath(\"~/language/\" + Application[\"xmlpath\"].ToString() + Request.Url.LocalPath + \".xml\"));";
                            }
                        }
                        else
                        {
                            string[] txtarr = bf[0].Split(new string[] { " Text=", " text=", " Text =", " text =" }, StringSplitOptions.None);
                            if (txtarr.Length >= 2)
                            {
                                string[] txtarr2 = txtarr[1].Split(new char[] { '"' });
                                txtvalue = txtarr2[1].Trim();
                                if (XMLUltility.SetValue("item", s[i - 1], txtvalue, defaultpath) == true)  // just add one more
                                {
                                    asp += "this." + s[i - 1] + ".Text=MultyLanguage.NetUtil.Translate(\"" + s[i - 1] + "\",Server.MapPath(\"~/language/\" + Application[\"xmlpath\"].ToString() + Request.Url.LocalPath + \".xml\"));";
                                }
                            }
                           
                           
                        }

                    }
                    else
                    {
                        s[i - 1] = "undefined";
                    }
                }
                if (asp != "<%")
                {
                    asp += "%>";
                    source = asp + source;
                    StreamWriter strw = new StreamWriter(path);
                    strw.Write(source);
                    strw.Close();
                }
                return s;
            }
            return null;
        }
        /// /// Reads the contents of a web page. ////// /// The URL of the page to read. /// The web page's contents.
        public static string ReadPage(string url)
        {

            Uri r = new Uri(url);
            WebRequest request = WebRequest.Create(r);
            WebResponse response = request.GetResponse();
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
            string responseText = readStream.ReadToEnd();
            readStream.Close(); response.Close();
            return responseText;
        } //////  /// Gets the contents of an html document, without the html tags. /// /// The original html. /// The contents of the html document, without the html tags. 
        public static string StripHtmlTags(string html)
        {
            return Regex.Replace(html, @"<(.|\n)*?>", string.Empty);
        } //////  /// Gets the HostName of an IP address. /// /// The source IP address. /// The HostName of the IP address. 
        public static string GetHostName(string ip)
        {
            IPHostEntry ipEntry = Dns.GetHostByAddress(ip);
            return ipEntry.HostName.ToString();
        }

        public static string GetXmlNodeValue(string xml, string selectExpressionOfDocumentElement)
        {
            XmlDocument dom = new XmlDocument();
            dom.LoadXml(xml);
            XmlNode node = dom.DocumentElement.SelectSingleNode(selectExpressionOfDocumentElement);

            if (null != node)
            {
                return node.InnerText;
            }
            else
            {
                return string.Empty;
            }
        }
        public static string Translate(string id, string xmlpath)
        {
            XmlDocument resource = new XmlDocument();
            resource.Load(xmlpath);
            XmlElement root = resource.DocumentElement;
            foreach (XmlElement n in root.ChildNodes)
            {
                if (n.Attributes["id"].Value == id)
                    return n.InnerText;
            }
            return "Undefined";
        }

    }
}
